package com.lindenvalley.de;

import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lindenvalley.de.achartengine.AverageTemperatureChart;
import com.lindenvalley.de.achartengine.XYChartBuilder;
import com.lindenvalley.de.adapter.ThemeArrayAdapter;
import com.lindenvalley.de.chartgraph.GraphViewDemo;
import com.lindenvalley.de.content.Sections;
import com.lindenvalley.de.content.user.User;
import com.lindenvalley.de.network.WorkServer;
import com.lindenvalley.de.parsers.SimpleXmlParser;

public class MainActivity extends Activity {
	
	public static final String EXTRA_THEME_NUMBER = "extraThemeNumber";
	public static final String EXTRA_ROUND_NUMBER = "extraRoundNumber";
	public static final String EXTRA_EXAM_MODE = "extraExameMode";
	private Sections sections;
	private boolean isExamMode;
	private TextView headerView;
	private ThemeArrayAdapter adapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        headerView = (TextView) findViewById(R.id.mainHeaderView);

//        Object result = WorkServer.getInstance().doLogin("tester@tester.com", "tester");
        
        
//        if (result instanceof User){
//        	WorkServer.getInstance().setUser((User)result);
//        }
        sections = WorkServer.getInstance().getSections();
        ListView lv = (ListView)findViewById(R.id.listView1);
        adapter = new ThemeArrayAdapter(this, R.layout.main_list_row, sections.getSection());
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(MainActivity.this, LearnActivity.class);
				
				intent.putExtra(EXTRA_THEME_NUMBER, sections.getSection().get(position).getId());
				intent.putExtra(EXTRA_EXAM_MODE, isExamMode);
				startActivity(intent);
			}
        	
		});
        
        setHeaderText();
    }
    
    /* (non-Javadoc)
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		Intent intent;
		switch (id) {
		case R.id.itemSync:
			intent = new Intent(this, SignActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.itemLearn:
			isExamMode = false;
			adapter.setIsExamMode(isExamMode);
			setHeaderText();
			break;
		case R.id.itemExam:
			isExamMode = true;
//			adapter.setIsExamMode(isExamMode);
//			setHeaderText();
			intent = new Intent(this, ExamActivity.class);
			startActivity(intent);
			break;
		case R.id.itemStat:
			intent = new Intent(this, XYChartBuilder.class);
			startActivity(intent);
			break;
		case R.id.itemShare:
			intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.shareText));
			startActivity(Intent.createChooser(intent, null));
			break;

		}
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (adapter != null)
			adapter.notifyDataSetChanged();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        //TODO it's not using now
        return true;
	}
	
	private void setHeaderText(){
		if (isExamMode){
			headerView.setText(R.string.menu_exam);
		} else {
			headerView.setText(R.string.menu_learn);
		}
		
	}
	
  
}